﻿using GalaSoft.MvvmLight;
using Services.Model;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Linq;
using Books.ViewModel.Paging;
using Books.Common;
using Books.View;
using Microsoft.Practices.ServiceLocation;

namespace Books.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase, IMainViewModel
    {
        private IBookDetailViewModel bookDetail
        {
            get { return ServiceLocator.Current.GetInstance<IBookDetailViewModel>(); }
        }
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            InitData();
           
        }

        public async void InitData()
        {
            GetDataHotBooks("100");
            GetNewestBooks("10");
            GetGenresBooks();
            ListDataAllBooks = await GetAllBooks(string.Empty, "100", "0");
            if (ListDataAllBooks.Count > 0)
                ListDataAllBooks = new ObservableCollection<BookList>(ListDataAllBooks.Take(18));
        }
        public void GetBookDetail(string bookId, string deviceId)
        {

            bookDetail.SetDataBookDetail(bookId, deviceId);
            App.rootFrame.Navigate(typeof(DetailPage));
        }

        private ObservableCollection<GenreList> listDataGenresBooks;
        public ObservableCollection<GenreList> ListDataGenresBooks
        {
            get
            {
                return listDataGenresBooks;
            }
            set
            {
                listDataGenresBooks = value;
                RaisePropertyChanged("ListDataGenresBooks");
            }
        }

        private ObservableCollection<BookList> listHotBooks;
        public ObservableCollection<BookList> ListHotBooks
        {
            get
            {
                return listHotBooks;
            }
            set
            {
                listHotBooks = value;
                RaisePropertyChanged("ListHotBooks");
            }
        }
        private ObservableCollection<BookList> listDataAllBooks;
        public ObservableCollection<BookList> ListDataAllBooks
        {
            get
            {
                return listDataAllBooks;
            }
            set
            {
                listDataAllBooks = value;
                RaisePropertyChanged("ListDataAllBooks");
            }
        }
        private ObservableCollection<BookList> listDataNewestBooks;
        public ObservableCollection<BookList> ListDataNewestBooks
        {
            get
            {
                return listDataNewestBooks;
            }
            set
            {
                listDataNewestBooks = value;
                RaisePropertyChanged("ListDataNewestBooks");
            }
        }
       
        private async Task<ObservableCollection<BookList>> GetDataHotBooks(string limit)
        {
            var getData = await App.CallService.GetAllHotBooksAsync(limit);
            if (getData != null && getData.book_list.Count > 0)
                ListHotBooks = new ObservableCollection<BookList>(getData.book_list);
            return ListHotBooks;
        }
        private async Task<ObservableCollection<BookList>> GetAllBooks(string deviceId, string pageSize, string pageIndex)
        {
            var getData = await App.CallService.GetAllBooksDataAsync(deviceId, pageSize, pageIndex);
            if (getData != null && getData.book_list.Count > 0)
                    return new ObservableCollection<BookList>(getData.book_list);
            return new ObservableCollection<BookList>();
        }
        private async Task<ObservableCollection<BookList>> GetNewestBooks(string limit)
        {
            var getData = await App.CallService.GetWestBooksAsync(limit);
            if (getData != null && getData.book_list.Count > 0)
                ListDataNewestBooks = new ObservableCollection<BookList>(getData.book_list);
            return ListDataNewestBooks;
        }
        private async Task<ObservableCollection<GenreList>> GetGenresBooks()
        {
            var getData = await App.CallService.GetGenresDataAsync();
            if (getData != null && getData.genre_list.Count > 0)
                ListDataGenresBooks = new ObservableCollection<GenreList>(getData.genre_list);
            return ListDataGenresBooks;
        }
       
        public async Task<ObservableCollection<BookList>> GetDataPage(Paging.PageType pageType, string pageIndex, string pageSize)
        {
            switch (pageType)
            {
                case PageType.AllBooks:
                    return await GetAllBooks(string.Empty,pageSize ,pageIndex );
                    break;
                case PageType.WestBooks:
                    return await GetNewestBooks(pageSize);
                    break;
                case PageType.HotBooks:
                    return await GetDataHotBooks(pageSize);
                    break;
                
            }
            return null;
        }

        BookDetail playNow;
        public BookDetail PlayNow
        {
            get
            {
                if (playNow == null)
                    playNow = new BookDetail();
                return playNow;
            }
            set
            {
                playNow = value;
                RaisePropertyChanged("PlayNow");
            }
        }
    }
}